//
//  NewFriendViewController.m
//  PigMarket
//
//  Created by mac on 2019/6/11.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#pragma mark --------- 新的囤友


#import "NewFriendViewController.h"
#import "ChatListCell.h"

@interface NewFriendViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
 myTableView
 */
@property (nonatomic , strong) UITableView *myTableView;
/**
 dataArr
 */
@property (nonatomic , strong) NSMutableArray *dataArr;
@end

@implementation NewFriendViewController


#pragma mark - view life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.baseNavView.navTitleLabel.text = @"新的豚友";
    [self setUpNav];
    [self setUpView];
}


#pragma mark - init method
- (void)setUpNav {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
}
- (void)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.myTableView];
}

- (void) initLayout {
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(TJ_NavBarHeight);
        make.right.left.bottom.mas_equalTo(0);
    }];
}

- (void) initAction {

}

- (void)initData {
    //获取未决好友列表
    uint64_t page = 1;
    TIMFriendPendencyRequest *req = TIMFriendPendencyRequest.new;
    req.numPerPage = page;
    req.type = TIM_PENDENCY_COME_IN;
    [[TIMFriendshipManager sharedInstance] getPendencyList:req succ:^(TIMFriendPendencyResponse *pendencyResponse) {
        [self.dataArr addObjectsFromArray:pendencyResponse.pendencies];
        [self.myTableView reloadData];
    } fail:^(int code, NSString *msg) {
        
    }];
}

#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChatListCell *cell = [ChatListCell cellWithTableView:tableView];
    TIMFriendPendencyItem *model = self.dataArr[indexPath.row];
    cell.addNewFriendModel = model;
    UIButton *tempBtn = cell.addStatusBtn;
   //同意添加好友
    @weakify(self);
    [cell setAgreeAddBlock:^{
        @strongify(self);
        [self agreeAddFriendWithIdentifier:model.identifier addBtn:tempBtn];
    }];
    return cell;
}

#pragma mark - target action

#pragma mark - private method
//同意对方添加好友的请求
- (void)agreeAddFriendWithIdentifier:(NSString *)identifier addBtn:(UIButton *)addBtn{

    TIMFriendResponse *rsp = TIMFriendResponse.new;
    rsp.identifier = identifier;
    rsp.responseType = TIM_FRIEND_RESPONSE_AGREE_AND_ADD;
    @weakify(self);
    [[TIMFriendshipManager sharedInstance] doResponse:rsp succ:^(TIMFriendResult *result) {
        @strongify(self);
        
     } fail:^(int code, NSString *msg) {
        [self toastWithContent:msg];
    }];
}
- (void)agreeAddFriendRequestWithIdentifier:(NSString *)identifier addBtn:(UIButton *)addBtn{
    NSString *memberNum = [UserInfoDataProvider sharedInstance].userInfo.memberNum;
    @weakify(self);
    [[[PigMarketApi sharedInstance] agreeAddFriendWithFromMemberNum:identifier toMemberNum:memberNum] subscribeNext:^(NSNull * _Nullable x) {
       
        @strongify(self);
        [addBtn setTitle:@"已添加" forState:UIControlStateNormal];
        addBtn.layer.borderColor = [UIColor whiteColor].CGColor;
        [addBtn setTitleColor:[UIColor colorWithHexString:@"#B4B4B4"] forState:UIControlStateNormal];
        
        //发送通知，通知好友列表通讯录刷新数据
        [[NSNotificationCenter defaultCenter] postNotificationName:NotificationAgreeAddFriendChange object:self];
    } error:^(NSError * _Nullable error) {
        
        @strongify(self);
        [self handlingError:error];

    }];
}
#pragma mark - getter and setter
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.rowHeight = 70;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
    }
    return _myTableView;
}
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}


@end
